home *** CD-ROM | disk | FTP | other *** search
- /* DesktopSwitch.c – Move files to and from the desktop folder using preconfigured sets.
-
- to do:
-
- Allow saving of sets to disk.
- Allow user customization of sets, hiding prefs, etc.
- */
-
- #include <Quickdraw.h>
- #include <Controls.h>
- #include <Dialogs.h>
- #include "DialogRoutines.h"
- #include "DesktopSwitch.h"
- #include "FileRoutines.h"
- #include "UIRoutines.h"
-
- PrefsHandle prefs;
-
-
-
- /*-----------------------------------PutAwayAllItemsInFolder()-----------------------------------*/
- //move everything in the source directory to the destination directory
- OSErr PutAwayAllItemsInFolder(short theVRefNum, long theDirID)
- {
- CInfoPBRec myPBRec;
- OSErr theErr;
- FSSpec theFile;
- Str255 myName;
- unsigned short flags;
- OSType type,creator;
- EventRecord theEvent;
-
- myPBRec.hFileInfo.ioNamePtr = (StringPtr) NewPtr (sizeof(Str255));
- myPBRec.hFileInfo.ioFDirIndex = 1;
- //Loop through all items in the directory
- do {
- myPBRec.hFileInfo.ioVRefNum = theVRefNum;
- myPBRec.hFileInfo.ioDirID = theDirID;
- myPBRec.hFileInfo.ioCompletion = NULL;
- theErr = PBGetCatInfo (&myPBRec,false);
- if (theErr == noErr){
- theErr = FSMakeFSSpec (theVRefNum,theDirID,
- myPBRec.hFileInfo.ioNamePtr,&theFile); //Create FSSpec to the item
- if (theErr == noErr) {
- flags = myPBRec.hFileInfo.ioFlFndrInfo.fdFlags;
- type = myPBRec.hFileInfo.ioFlFndrInfo.fdType;
- creator = myPBRec.hFileInfo.ioFlFndrInfo.fdCreator;
- //only move unlocked, non-invisible files who aren't DesktopSwitch files
- if (!(flags & fInvisible) && !(flags & fNameLocked) &&
- !(creator == myCreator)) {
- if ((**prefs).PutAway){
- theErr = PutAwayItem(theFile);
- if (theErr) {
- //advance to the next file, reset the error
- myPBRec.hFileInfo.ioFDirIndex++;
- theErr = noErr;
- DebugStr ("\pCouldn't move file.");
- }
- }
- if ((**prefs).MakeInvis) {
- MakeInvisible(theFile);
- myPBRec.hFileInfo.ioFDirIndex++;
- theErr = noErr;
- }
- }
- else
- myPBRec.hFileInfo.ioFDirIndex++;
- }
- else
- DebugStr ("\pFailed to create FSSpec to folder item.");
- }
- else
- if (theErr != fnfErr) DebugStr ("\pPBGetCatInfo failed.");
- } while (theErr == noErr);
- DisposPtr((Ptr)myPBRec.hFileInfo.ioNamePtr);
- //Dont consided fnfErr a real error since it only indicates that we have looped
- //through all items
- if (theErr == fnfErr)
- return noErr;
- else
- return theErr;
- }
-
- OSErr PutAwayItem (FSSpec theItem)
- {
- FXInfo myFXInfo;
- long awayDir;
- Str31 name;
- long dirID,errDirID;
- FSSpec destSpec,junkFile,errFolder;
- OSErr err;
- Str255 tempStr;
-
- //2 for dirid means root dir
- err = FSMakeFSSpec (theItem.vRefNum,2,(**prefs).folderName,&errFolder);
- if (err)
- FSpDirCreate (&errFolder,0,&errDirID);
- tempStr[0] = (**prefs).folderName[0] +2;
- BlockMove (&(**prefs).folderName[1],&tempStr[2],(**prefs).folderName[0]);
- tempStr[1] = ':';
- tempStr[tempStr[0]] = ':';
- BlockMove (&theItem.name[1],&tempStr[tempStr[0]+1],theItem.name[0]);
- tempStr[0] += theItem.name[0];
- FSMakeFSSpec (theItem.vRefNum,2,tempStr,&errFolder);
- GetFinderInfo (theItem,NULL,&myFXInfo);
- if (myFXInfo.fdPutAway == 0) { //nowhere to put it
- if ((**prefs).MakeInvisWNF) {
- MakeInvisible (theItem);
- err = noErr;
- goto done;
- }
- else
- awayDir = errFolder.parID;
- }
- else
- awayDir = myFXInfo.fdPutAway;
- err = FSMakeFSSpec(theItem.vRefNum,awayDir,0,&destSpec);
- if (err) {
- if ((**prefs).MakeInvisWNF) {
- MakeInvisible (theItem);
- err = noErr;
- goto done;
- }
- else {
- awayDir = errFolder.parID;
- FSMakeFSSpec (theItem.vRefNum,awayDir,0,&destSpec);
- }
- }
- //this is strange – if we dont get an error here, that means a file by the same name already
- //exists, so we'll just move this file to the root dir
- if (! FSMakeFSSpec (theItem.vRefNum,awayDir,theItem.name,&junkFile))
- if ((**prefs).MakeInvisWNF) {
- MakeInvisible (theItem);
- err = noErr;
- goto done;
- }
- else {
- awayDir = errFolder.parID;
- FSMakeFSSpec (theItem.vRefNum,awayDir,0,&destSpec);
- }
- err = FSpCatMove(&theItem,&destSpec);
- done:
- return err;
- }
-
-
- /*-----------------------------------HideItems()-----------------------------------*/
-
- //put all items currently in the desktop folder away
- void HideItems()
- {
- short vref;
- long ddirid,oldddirid;
- OSErr err;
- VCB * p;
-
- for (p = myVCBQHdr;p != NULL;p = (VCB *)p->qLink) {
-
- vref = p->vcbVRefNum;
- err = FindFolder (vref,kDesktopFolderType,true,&vref,&ddirid);
- if (err) goto done;
- PutAwayAllItemsInFolder (vref,ddirid);
- PutAwayAllItemsInFolder (vref,ddirid); //for some reason, we miss the last
- //item otherwise
- }
-
- done:
- //all done
- return;
- }
-
- OSErr CreateDesktopFileList (FileListHandle *theDTFiles)
- {
- short vref;
- long ddirid;
- FSSpec theFile;
- CInfoPBRec myPBRec;
- unsigned short flags;
- OSErr err;
-
- err = FindFolder (kOnSystemDisk,kDesktopFolderType,true,&vref,&ddirid);
- if (err)
- goto done;
- *theDTFiles = (FileListHandle)NewHandle(sizeof(short)); //only a file count
- (***theDTFiles).numFiles = 0;
- myPBRec.hFileInfo.ioNamePtr = (StringPtr) NewPtr (sizeof(Str255));
- myPBRec.hFileInfo.ioFDirIndex = 1;
- //Loop through all items in the directory
- do {
- myPBRec.hFileInfo.ioVRefNum = vref;
- myPBRec.hFileInfo.ioDirID = ddirid;
- myPBRec.hFileInfo.ioCompletion = NULL;
- err = PBGetCatInfo (&myPBRec,false);
- if (err == noErr){
- err = FSMakeFSSpec (vref,ddirid,
- myPBRec.hFileInfo.ioNamePtr,&theFile); //Create FSSpec to the item
- if (err == noErr) {
- flags = myPBRec.hFileInfo.ioFlFndrInfo.fdFlags;
- //only include non-invisible files
- if (!(flags & fInvisible)){
- SetHandleSize((Handle)*theDTFiles,
- GetHandleSize((Handle)*theDTFiles) + sizeof(Handle));
- if (MemError()) DebugStr ("\pCouldn't upsize DT file handle.");
- (***theDTFiles).numFiles++;
- err = NewAlias (NULL,
- &theFile,
- &(***theDTFiles).theFiles[(***theDTFiles).numFiles-1]);
- if (err) DebugStr ("\pNewAlias() failed.");
- }
- myPBRec.hFileInfo.ioFDirIndex++;
- }
- else
- DebugStr ("\pFailed to create FSSpec to folder item.");
- }
- else
- if (err != fnfErr) DebugStr ("\pPBGetCatInfo failed.");
- } while (err == noErr);
- DisposPtr((Ptr)myPBRec.hFileInfo.ioNamePtr);
- //Dont consided fnfErr a real error since it only indicates that we have looped
- //through all items
- done:
- if (err == fnfErr)
- return noErr;
- else
- return err;
-
- }
-
-
- OSErr SaveDesktopFile (FSSpec theFile)
- {
- FileListHandle theFileList;
- FileListRsrcHandle fListRsrcHndl;
- short rnum,newid,i;
- Handle h;
- FSSpec theSpec;
- Boolean changed;
- FInfo myFInfo;
-
- CreateDesktopFileList (&theFileList);
- rnum = FSpOpenResFile(&theFile,fsRdWrPerm);
- if (rnum <= 0) {
- DebugStr ("\pFailed to open file.");
- goto done;
- }
- fListRsrcHndl = (FileListRsrcHandle)NewHandle(sizeof(MyFInfoStruct) * ((**theFileList).numFiles) + sizeof(short));
- if (!fListRsrcHndl) goto done;
- (**fListRsrcHndl).numFiles = (**theFileList).numFiles;
- for (i=0;i<(**theFileList).numFiles;i++){
- newid = Unique1ID ('alis');
- if (ResolveAlias(NULL,
- (**theFileList).theFiles[i],
- &theSpec,
- &changed)) continue;
- GetFinderInfo (theSpec,&myFInfo,NULL);
- (**fListRsrcHndl).fileList[i].filePos = myFInfo.fdLocation;
- (**fListRsrcHndl).fileList[i].aliasID = newid;
- h = (Handle)(**theFileList).theFiles[i];
- AddResource (h,'alis',newid,"\p");
- WriteResource (h);
- DetachResource (h);
- DisposHandle (h);
- }
- AddResource ((Handle)fListRsrcHndl,'flst',128,"\p");
- WriteResource ((Handle)fListRsrcHndl);
- ReleaseResource ((Handle)fListRsrcHndl);
- DisposHandle ((Handle)theFileList);
- done:
- CloseResFile (rnum);
- return ResError();
- }
-
- //load a desktop file from disk, remove old files, move new ones in
- OSErr LoadDesktopFile (FSSpec theFile)
- {
- FileListRsrcHandle dTopFiles;
- short i;
- FSSpec theSpec;
- Boolean changed;
- OSErr err;
- short rnum;
- Handle h;
- FInfo myFInfo;
- FXInfo myFXInfo;
- EventRecord myEvt;
-
- //make everything else go away
- HideItems();
- WNEDelay (30);
- //first, load according to desktop file resource
- rnum = FSpOpenResFile (&theFile,fsRdWrPerm);
- dTopFiles = (FileListRsrcHandle)GetResource ('flst',128);
- if (!dTopFiles) goto done;
- for (i=0;i<(**dTopFiles).numFiles;i++) {
- h = GetResource('alis',(**dTopFiles).fileList[i].aliasID);
- DetachResource (h);
- if (ResolveAlias(NULL,
- (AliasHandle)h,
- &theSpec,
- &changed)) continue;
- DisposHandle (h);
- GetFinderInfo (theSpec,&myFInfo,&myFXInfo);
- myFInfo.fdLocation = (**dTopFiles).fileList[i].filePos;
- myFXInfo.fdPutAway = theSpec.parID;
- SetFinderInfo (theSpec,&myFInfo,&myFXInfo);
- MoveFileToDesktop(theSpec);
- }
- ReleaseResource ((Handle)dTopFiles);
- CloseResFile (rnum);
- done:
- return err;
- }
-
-
-
- OSErr CreateDTFileSet(void)
- {
- StandardFileReply theReply;
- OSErr err;
-
- StandardPutFile ("\pName of new desktop set:","\pDesktopSet",&theReply);
- if (theReply.sfGood) {
- if (theReply.sfReplacing)
- FSpDelete(&theReply.sfFile);
- FSpCreateResFile (&theReply.sfFile,'DTSW','dset',theReply.sfScript);
- if (! ResError()) {
- SaveDesktopFile (theReply.sfFile);
- }
- else
- DebugStr ("\pFailed to save desktop file.");
- }
-
- }
-
-
- void UpdatePrefsDialogForSettings(DialogPtr theDialog)
- {
- Rect iRect;
- Handle iHandle;
- short iType;
-
- if ((**prefs).PutAway){
- GetDItem (theDialog,kPrefsDialog_MakeInvisible,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_PutAway,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,1);
- GetDItem (theDialog,kPrefsDialog_MoveToFolder,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_MakeInvisWNF,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,0);
- } else
- if ((**prefs).MakeInvis) {
- GetDItem (theDialog,kPrefsDialog_PutAway,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_MakeInvisible,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,1);
- GetDItem (theDialog,kPrefsDialog_MoveToFolder,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,255);
- GetDItem (theDialog,kPrefsDialog_FolderButton,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,255);
- GetDItem (theDialog,kPrefsDialog_FolderText,&iType,&iHandle,&iRect);
- //HiliteControl ((ControlHandle)iHandle,255);
- SetIText (iHandle,"\p");
- GetDItem (theDialog,kPrefsDialog_MakeInvisWNF,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,255);
- }
- if ((**prefs).MoveToFolder && (**prefs).PutAway){
- GetDItem (theDialog,kPrefsDialog_MoveToFolder,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,1);
- GetDItem (theDialog,kPrefsDialog_MakeInvisWNF,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_FolderButton,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_FolderText,&iType,&iHandle,&iRect);
- //HiliteControl ((ControlHandle)iHandle,0);
- SetIText (iHandle,(**prefs).folderName);
- }
- else if ((**prefs).MakeInvisWNF && (**prefs).PutAway) {
- GetDItem (theDialog,kPrefsDialog_MoveToFolder,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,0);
- GetDItem (theDialog,kPrefsDialog_MakeInvisWNF,&iType,&iHandle,&iRect);
- SetCtlValue ((ControlHandle)iHandle,1);
- GetDItem (theDialog,kPrefsDialog_FolderButton,&iType,&iHandle,&iRect);
- HiliteControl ((ControlHandle)iHandle,255);
- GetDItem (theDialog,kPrefsDialog_FolderText,&iType,&iHandle,&iRect);
- //HiliteControl ((ControlHandle)iHandle,255);
- SetIText (iHandle,"\p");
- }
- }
-
- void AskUserForFolder()
- {
- DialogPtr theDialog;
- short theItem;
- short iType;
- Rect iRect;
- Handle iHand;
- Str31 iText;
-
- theDialog = GetNewDialog (kGetFolderNameDialogID,NULL,(DialogPtr)-1L);
- GetDItem (theDialog,kFolderNameDialog_Name,&iType,&iHand,&iRect);
- SetIText (iHand,(**prefs).folderName);
- do {
- ModalDialog ((ProcPtr)StdFilterProc,&theItem);
- } while (theItem != kFolderNameDialog_OK && theItem != kFolderNameDialog_Cancel);
- if (theItem == kFolderNameDialog_OK) {
- GetIText (iHand,iText);
- BlockMove (iText,(**prefs).folderName,LENGTH(iText)+1);
- }
- DisposeDialog (theDialog);
- }
-
- void ConfigurePrefs ()
- {
- DialogPtr theDialog;
- Rect iRect;
- Handle iHandle;
- short iType;
- short theItem;
- PrefsStruct oldPrefs;
-
- theDialog = GetNewDialog (kPrefsDialogID,NULL,(DialogPtr)-1L);
- SetPort ((GrafPtr)theDialog);
- GetDItem (theDialog,kPrefsDialog_UI1,&iType,&iHandle,&iRect);
- FrameRect (&iRect);
- GetDItem (theDialog,kPrefsDialog_UI2,&iType,&iHandle,&iRect);
- FrameRect (&iRect);//this wont work
- oldPrefs = (**prefs);
- UpdatePrefsDialogForSettings(theDialog);
- do {
- ModalDialog((ProcPtr)StdFilterProc,&theItem);
- switch (theItem) {
- case kPrefsDialog_PutAway:
- if ((**prefs).PutAway)
- break;
- (**prefs).PutAway = true;
- (**prefs).MakeInvis = false;
- UpdatePrefsDialogForSettings(theDialog);
- break;
- case kPrefsDialog_MakeInvisible:
- if ((**prefs).MakeInvis)
- break;
- (**prefs).MakeInvis = true;
- (**prefs).PutAway = false;
- UpdatePrefsDialogForSettings(theDialog);
- break;
- case kPrefsDialog_MoveToFolder:
- if ((**prefs).MoveToFolder)
- break;
- (**prefs).MoveToFolder = true;
- (**prefs).MakeInvisWNF = false;
- UpdatePrefsDialogForSettings(theDialog);
- break;
- case kPrefsDialog_MakeInvisWNF:
- if ((**prefs).MakeInvisWNF)
- break;
- (**prefs).MakeInvisWNF = true;
- (**prefs).MoveToFolder = false;
- UpdatePrefsDialogForSettings(theDialog);
- break;
- case kPrefsDialog_FolderButton:
- AskUserForFolder();
- GetDItem (theDialog,kPrefsDialog_UI1,&iType,&iHandle,&iRect);
- FrameRect (&iRect);
- GetDItem (theDialog,kPrefsDialog_UI2,&iType,&iHandle,&iRect);
- FrameRect (&iRect);//this wont work
- DrawDialog (theDialog);
- UpdatePrefsDialogForSettings (theDialog);
- break;
- }
- } while (theItem != kPrefsDialog_OK && theItem != kPrefsDialog_Cancel);
- if (theItem == kPrefsDialog_Cancel)
- (**prefs) = oldPrefs;
- else
- SavePrefs();
- DisposeDialog (theDialog);
-
- }
-
-
- void LoadPrefs ()
- {
- short vref,rnum;
- long dirid;
- FSSpec prefsFile;
- OSErr err;
- Str255 name;
-
- FindFolder(kOnSystemDisk,kPreferencesFolderType,true,&vref,&dirid);
- err = FSMakeFSSpec (vref,dirid,"\pDesktopSwitch Prefs",&prefsFile);
- if (err) {
- FSpCreateResFile (&prefsFile,'DTSW','dspr',0);
- }
- rnum = FSpOpenResFile (&prefsFile,fsRdWrPerm);
- if (rnum <= 0)
- DebugStr ("\pCouldnt create prefs.");
- prefs = (PrefsHandle)Get1Resource(kPrefsType,kPrefsID);
- if (! prefs) {
- prefs = (PrefsHandle)NewHandle(sizeof(PrefsStruct));
- if (!prefs)
- DebugStr ("\pCouldnt get memory for prefs data.");
- (**prefs).MakeInvis = false;
- (**prefs).PutAway = true;
- (**prefs).MoveToFolder = false;
- (**prefs).MakeInvisWNF = true;
- BlockMove (kDefaultFolderName,(**prefs).folderName,LENGTH(kDefaultFolderName)+1);
- AddResource ((Handle)prefs,kPrefsType,kPrefsID,"\p");
- }
- DetachResource ((Handle)prefs);
- CloseResFile (rnum);
- }
-
- void SavePrefs ()
- {
- short vref,rnum;
- long dirid;
- FSSpec prefsFile;
- OSErr err;
- Handle h;
-
- FindFolder(kOnSystemDisk,kPreferencesFolderType,true,&vref,&dirid);
- err = FSMakeFSSpec (vref,dirid,"\pDesktopSwitch Prefs",&prefsFile);
- if (err) {
- FSpCreateResFile (&prefsFile,'DTSW','dspr',0);
- }
- rnum = FSpOpenResFile (&prefsFile,fsRdWrPerm);
- h = Get1Resource(kPrefsType,kPrefsID);
- if (h) {
- RmveResource (h);
- DisposHandle (h);
- }
- AddResource ((Handle)prefs,kPrefsType,kPrefsID,"\p");
- DetachResource ((Handle)prefs);
- CloseResFile (rnum);
- }
-
- void AskUserForDTFile() {
- SFTypeList types;
- StandardFileReply reply;
-
- types[0] = 'dset';
- StandardGetFile (NULL,1,types,&reply);
- if (reply.sfGood) {
- LoadDesktopFile(reply.sfFile);
- }
- }
-
- main()
- {
- short numFiles;
- short whatToDo;
- FSSpec spec;
-
- InitProg();
- /* CountAppFiles (&whatToDo,&numFiles);
- if (numFiles && !whatToDo) {
- AppFile theFile;
-
- GetAppFiles (1,&theFile);
- FSMakeFSSpec (theFile.vRefNum,0,theFile.fName,&spec);
- LoadDesktopFile (spec);
- ExitToShell();
- } */
- mel();
- }
-